<template>
  <div>
    <el-card v-loading="collectLoading" class="sun-el-card">
      <div style="font-weight: bold;font-size: 18px;margin-bottom: 14px;">数据采集设置</div>
      <el-form
        ref="collectFormData"
        :model="collectFormData"
        label-position="left"
        label-width="150px"
      >
        <el-form-item
          label="是否采集用户信息"
        >
          <el-switch v-model="collectFormData.collect_user_info" />
        </el-form-item>

        <el-form-item
          label="是否采集评论回复"
        >
          <el-switch v-model="collectFormData.collect_comment_reply" />
        </el-form-item>

      </el-form>

      <div class="flex_end">
        <el-button size="small" type="primary" style="width: 100px;font-size: 14px;" @click="collectOnFinish">提 交</el-button>
      </div>
    </el-card>

    <el-card v-loading="collectLoading2" class="sun-el-card">
      <div style="font-weight: bold;font-size: 18px;margin-bottom: 14px;">情感分析设置</div>
      <el-form
        ref="collectFormData2"
        :model="collectFormData2"
        label-position="left"
        label-width="150px"
      >
        <el-form-item
          label="潜在客户评论分数线"
          prop="score_line"
          :rules="[{ required: true, message: '0-1', trigger: ['blur', 'change'] }]"
        >
          <el-input v-model="collectFormData2.score_line" style="width: 180px;" placeholder="请输入分数（0-1之间）" />
        </el-form-item>

        <el-form-item
          label="是否允许修改分数线"
        >
          <el-switch v-model="collectFormData2.edit_comment_score" />
        </el-form-item>

      </el-form>

      <div class="flex_end">
        <el-button size="small" type="primary" style="width: 100px;font-size: 14px;" @click="collect2OnFinish">提 交</el-button>
      </div>
    </el-card>
  </div>
</template>

<script>
import { put, get } from '@/api/request'
import { $tooltips } from '@/sun-components/SunMsg.js'
export default {
  name: 'Setting',
  components: {},
  inject: ['reload'],
  data() {
    return {
      collectFormData: {
        collect_user_info: false,
        collect_comment_reply: false
      },
      collectFormData2: {
        score_line: 0.7,
        edit_comment_score: false
      },
      collectLoading: false,
      collectLoading2: false
    }
  },
  created() {
    this.defaultData()
  },
  methods: {
    async collectOnFinish() {
      const _that = this
      this.$refs.collectFormData.validate(valid => {
        if (valid) {
          _that.collectLoading = true
          _that.collectFormData.type = 1
          put('system/settings', _that.collectFormData).then(response => {
            if (response.code === 0) {
              $tooltips(`设置成功!`, 'success')
            }
            _that.collectLoading = false
          })
        }
      })
    },
    async collect2OnFinish() {
      const _that = this
      this.$refs.collectFormData.validate(valid => {
        if (valid) {
          _that.collectLoading2 = true
          _that.collectFormData2.type = 2
          put('system/settings', _that.collectFormData2).then(response => {
            if (response.code === 0) {
              $tooltips(`设置成功!`, 'success')
            }
            _that.collectLoading2 = false
          })
        }
      })
    },
    async defaultData() {
      await get(`system/settings`).then(response => {
        if (response.code === 0) {
          const result = response.data
          this.collectFormData.collect_user_info = Number(result.spider_setting.collect_user_info.value) > 0
          this.collectFormData.collect_comment_reply = Number(result.spider_setting.collect_comment_reply.value) > 0
          this.collectFormData2.score_line = result.nlp_setting.score_line.value
          this.collectFormData2.edit_comment_score = Number(result.nlp_setting.edit_comment_score.value) > 0
          console.log(this.collectFormData2)
        }
      })
    }
  }
}
</script>
<style lang="scss" scoped>
  @import '@/styles/sun-style.scss';

  .el-form--label-left .el-form-item__label {
    text-align: right;
  }
</style>
